﻿using System;
using System.Reflection;
using System.Web.Mvc;

namespace CodeContrib.Web.Mvc
{
	[AttributeUsage(AttributeTargets.Method, Inherited = true)]
	public class WizardStepAttribute : ActionMethodSelectorAttribute
	{
		private readonly int _step;

		private static string WizardStepKey { get; set; }

		static WizardStepAttribute()
		{
			WizardStepKey = "step";
		}

		public WizardStepAttribute(int step)
		{
			_step = step;
		}

		public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
		{
			int step;

			return int.TryParse(controllerContext.RequestContext.HttpContext.Request[WizardStepKey], out step) && _step == step;
		}
	}
}